package com.tenebrous.jestic.controller.framwork.meber.author;

import com.tenebrous.jestic.compents.annotations.Weblog;
import com.tenebrous.jestic.compents.annotations.WeblogEnum;
import com.tenebrous.jestic.controller.SupperController;
import com.tenebrous.jestic.entity.framwork.meber.author.AuthRole;
import com.tenebrous.jestic.entity.framwork.meber.author.AuthRoleRouter;
import com.tenebrous.jestic.entity.framwork.meber.organ.OrgUser;
import com.tenebrous.jestic.service.framwork.meber.author.AuthRoleService;
import com.rain.ops.sdk.dto.ApiResult;
import com.rain.ops.sdk.utils.UUIDUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Date;

@Validated
@RestController
@RequestMapping("/author/role")
public class RoleController extends SupperController {

    @Autowired
    private AuthRoleService roleService;

    @PostMapping("/get")
    public ApiResult get(@NotBlank String id) {
        AuthRole role = roleService.selectByPrimaryKey(id);
        if(role != null) {
            return new ApiResult(200, "查询成功。", role);
        }
        return new ApiResult(999, "查询失败，未找到指定对象。");
    }

    @PostMapping("/get-role-router")
    public ApiResult getRoleRouter(@NotBlank String id) {
        AuthRoleRouter router = roleService.getRoleRouter(id);
        if(router != null) {
            return new ApiResult(200, "查询成功。", router);
        }
        return new ApiResult(999, "查询失败，未找到指定对象。");
    }

    /**
     * 查询角色
     * @return
     */
    @PostMapping("/paging")
    public ApiResult paging( String name) {
        return new ApiResult(200, "查询成功。", roleService.queryPagingList(super.getPageIndex(), super.getPageSize(), name));
    }

    /**
     * 分页查询角色用户集合
     * @param rid
     * @return
     */
    @PostMapping("/paging-role-users")
    public ApiResult pagingRoleUsers(@NotBlank String rid, String realname) {
        return new ApiResult(200, "查询成功。", roleService.queryPagingRoleUserList(super.getPageIndex(), super.getPageSize(), rid, realname));
    }

    /**
     *
     * @param userid
     * @param rolename
     * @return
     */
    @PostMapping("/paging-user-roles")
    public ApiResult queryUserRoles(@NotBlank String userid, String rolename) {
        return new ApiResult(200, "查询成功。", roleService.queryPagingUserRoleList(super.getPageIndex(), super.getPageSize(), userid, rolename));
    }

    /**
     * 查询当前登陆用户角色信息
     * @return
     */
    @PostMapping("/query-current-user-roles")
    public ApiResult queryCurrentUserRoles() {
        OrgUser ouser = super.getCurrentUser();
        return new ApiResult(200, "查询成功。", roleService.queryUserRoleList(ouser.getId()));
    }

    /**
     * 查询角色导航集合
     * @param rid
     * @return
     */
    @PostMapping("/query-role-menus")
    public ApiResult queryRoleMenus(@NotBlank String rid) {
        return new ApiResult(200, "查询成功。", roleService.queryRoleMenuList(rid));
    }

    /**
     * 查询角色用户集合
     * @param rid
     * @return
     */
    @PostMapping("/query-role-users")
    public ApiResult queryRoleUsers(@NotBlank String rid) {
        return new ApiResult(200, "查询成功。", roleService.queryRoleUserList(rid));
    }

    /**
     * 获取流程定义
     * @param roleid
     * @return
     */
//    @PostMapping("/query-role-process")
//    public ApiResult qieryProcessDefinitionList(@NotBlank String roleid) {
//        PageInfo pageInfo = roleService.queryProcessDefinitionPagingList(super.getPageIndex(), super.getPageSize() ,roleid);
//        return new ApiResult(200, "查询成功。", pageInfo);
//    }

    /**
     * 更新对象
     * @param item
     * @return
     */
    @PostMapping("/update")
    @Weblog(description = "更新角色信息", method = WeblogEnum.UPDATE)
    public ApiResult update(@NotNull AuthRole item) {
        Date dt = new Date();
        OrgUser ouser = super.getCurrentUser();
        if(StringUtils.isNotBlank(item.getId())) {
            item.setUpdateUserid(ouser.getId());
            item.setUpdateDate(dt);
            roleService.updateSelective(item);
        } else {
            item.setId(UUIDUtils.randomUUID());
            item.setCreateUserid(ouser.getId());
            item.setSort(dt.getTime());
            item.setCreateDate(dt);
            roleService.saveSelective(item);
        }
        return new ApiResult(200, "更新成功。");
    }

    /**
     * 更新角色导航信息
     * @param rid 角色ID
     * @param mids 导航ID集合
     * @return
     */
    @PostMapping("/update-role-menus")
    @Weblog(description = "更新角色导航信息", method = WeblogEnum.UPDATE)
    public ApiResult updateRoleMenus(@NotBlank String rid, @NotEmpty String[] mids) {

        return new ApiResult(200, "更新成功。", roleService.updateRoleMenus(rid, mids));
    }

    /**
     * 更新流程定义的对象 端详
     * @param roleid ,pids
     * @return
     */
    @PostMapping("update-role-process")
    @Weblog(description = "更新角色关联的流程定义信息", method = WeblogEnum.UPDATE)
    public ApiResult updateRoleProcess(@NotBlank String roleid, @NotEmpty String[] pids) {
        return new ApiResult(200, "更新成功。", roleService.updateRoleProcess(roleid, pids));
    }

    @PostMapping("/update-role-router")
    @Weblog(description = "更新角色关联的路由信息", method = WeblogEnum.UPDATE)
    public ApiResult updateRoleRouter(@NotNull @Validated AuthRoleRouter item) {
        return new ApiResult(200, "更新成功。", roleService.updateRoleRouter(item));
    }

    /**
     * 更新用户角色信息，清除原有信息。
     * @param uid
     * @param rids
     * @return
     */
    @PostMapping("/update-user-roles")
    @Weblog(description = "更新用户关联的角色信息", method = WeblogEnum.UPDATE)
    public ApiResult updateUserRoles(@NotBlank String uid, @NotEmpty String[] rids) {
        return new ApiResult(200, "更新成功。", roleService.updateUserRoles(uid, rids));
    }

    /**
     *  在原有信息基礎上添加新的角色信息。
     * @param uid
     * @param rids
     * @return
     */
    @PostMapping("/insert-user-roles")
    @Weblog(description = "添加用户关联的角色信息", method = WeblogEnum.UPDATE)
    public ApiResult insertUserRoles(@NotBlank String uid, @NotEmpty String[] rids) {
        return new ApiResult(200, "添加成功。", roleService.insertUserRoles(uid, rids));
    }

    /**
     * 更新角色用户信息，增量添加。
     * @param rid 角色ID
     * @param uids 用户ID集合
     * @return
     */
    @PostMapping("/insert-role-users")
    @Weblog(description = "更新角色关联的用户信息", method = WeblogEnum.UPDATE)
    public ApiResult insertRoleUsers(@NotBlank String rid, @NotEmpty String[] uids) {
        return new ApiResult(200, "更新成功。", roleService.insertRoleUsers(rid, uids));
    }

    /**
     * 更新流程定义的对象 端详
     * @param roleid ,pids
     * @return
     */
    @PostMapping("insert-role-process")
    @Weblog(description = "添加角色关联的流程定义信息", method = WeblogEnum.UPDATE)
    public ApiResult insertRoleProcess(@NotBlank String roleid, @NotEmpty String[] pids) {
        return new ApiResult(200, "添加成功。", roleService.insertRoleProcess(roleid, pids));
    }

    /**
     * 删除角色记录
     * @return
     */
    @PostMapping("/delete")
    @Weblog(description = "删除角色及其对应关联信息", method = WeblogEnum.DELETE)
    public ApiResult delete(@NotEmpty String[] ids) {
        return new ApiResult(200, "删除成功。", roleService.deleteByIds(ids));
    }

    /**
     * 删除角色用户记录
     * @return
     */
    @PostMapping("/delete-role-users")
    @Weblog(description = "删除角色关联的用户信息", method = WeblogEnum.DELETE)
    public ApiResult deleteRoleUsers(@NotBlank String rid, @NotEmpty String[] uids) {
        return new ApiResult(200, "删除成功。", roleService.deleteRoleUsers(rid, uids));
    }

    @PostMapping("/delete-role-process")
    @Weblog(description = "删除角色关联的流程定义信息", method = WeblogEnum.DELETE)
    public ApiResult deleteRoleProcess(@NotBlank String rid, @NotEmpty String[] pids) {
        return new ApiResult(200, "删除成功。", roleService.deleteRoleProcess(rid, pids));
    }

    /**
     * 删除用户角色信息
     * @param uid
     * @param rids
     * @return
     */
    @PostMapping("/delete-user-roles")
    @Weblog(description = "删除用户关联的角色信息", method = WeblogEnum.DELETE)
    public ApiResult deleteUserRoles(@NotBlank String uid, @NotEmpty String[] rids) {
        return new ApiResult(200, "删除成功。", roleService.deleteUserRoles(uid, rids));
    }

}
